CN.record = "个用户";
var swfu;
var msg=CN;
var ufile = {fid:0,filepath:"",name:""};
var userfile = {fid:0,selectfileid:0,name:"",path:"",target:""};
var objectModel = {edit_status:"create",uid:site.uid,rolecode:"editor"};
var colors = ["label-info","label-success","label-warning","label-important"];
var iscorrectEmail = false;
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            td = function(arg,title){
        		if(!arg)arg="-";
	        	if(title){
	        		return "<td title='"+title+"'>"+arg+"</td>";           
	        	}else{
	        		return "<td>"+arg+"</td>";
	        	}
            },
            setstatus = function(arg){
            	var keys = ["正常","停止","移除","4","4","4","4","4","9"];
            	return "<span class='label "+colors[arg]+"'>"+keys[arg-1]+"</span>";
            },
            setrole = function(key){
            	var text = "";
            	switch(key){
            		case "administrator":text="管理员";break;
            		case "editor":text="编辑";break;
            		case "author":text="作者";break;
            		case "contributor":text="投稿者";break;
            		case "subscriber":text="订阅者";break;
            	}
            	return "<span class='label "+colors[0]+"'>"+text+"</span>";
            },
            setButton = function(data){
            	if(data.status==3){
            		return "<span class='label'>移除状态</span>";
            	}
            	var btns = '<div class="btn-group" data-toggle="buttons-radio">';
            	btns += "<a class='btn btn-info btn-mini' onclick='del(this)' data-name='"+data.nickname+"' data-id='"+data.uid+"'>移除</a>";
            	btns += "</div>";
            	return btns;
            };
        for ( var element in data) {
        	var kk =data[element],
             	u_btn = '<button class="btn btn-info btn-mini" onclick="get(this)" type="button" data-id="'+kk.uid+'">'+kk.email+'</button>';
	        	tdValue =td(kk.uid)+ 
	        	td(kk.nickname)+
	        	td(u_btn)+
	        	td(kk.moblie)+ 
	        	td(setrole(kk.rolecode))+ 
	        	td(setstatus(kk.status))+
	        	td(timeformat(kk.addtime,4))+
	        	td(setButton(kk));
               	rowclass = element%2;//行样式
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
	mpage.init();
}); 

function init(){
	init_dialog();
	init_btn();
}

init_btn = function(){		
	$(".setrole").click(function(){
		objectModel.role = $(this).attr("key");
	});	
	
	$(".setstatus").click(function(){
		objectModel.status = $(this).attr("key");
	});	
	
	$("#email").blur(function(){
		var email = $(this).val();
		if(!validate(email,6)){
			$(".emsg").html("<span class='label'>邮箱格式有误</span>");
			return;
		}
		
		$(".emsg").html("<span class='label'>验证中...</span>");
		cb  = function(data){
			if(data.code==1){
				if(data.data>0){
					$(".emsg").html("<span class='label "+colors[3]+"'>邮箱已经注册，请选择新的邮箱</span>");
				}else{
					$(".emsg").html("<span class='label "+colors[1]+"'>邮箱可以注册</span>");
				}
			}
		};
		var json = {url:site.web+site.module+"/verifyemail",email:email};
		G(json,cb);
	});
}

init_dialog = function(){
	$("#dialog_create").click(function(){//create & open dialog
		objectModel.edit_status = "create";
		open_dialog();
	});
	$("#dialog_save").click(function(){
		
		var email = $("#email").val();
		if(!validate(email,6)){
			$(".dialogmsg").text("邮箱格式有误");
			return;
		}		
		
		$(this).attr("disabled",true);
		$(".dialogmsg").text("执行操作中...");
		$("#form2 input").each(function(){
			var id = $(this).attr("id");
			if(id)
				objectModel[id] =  $(this).val();
		});
		objectModel.url = site.web+site.module+"/"+objectModel.edit_status;//包含create/update操作
		
		
		//TODO 20131016
		//validate email again 
		
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				mpage.init();
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);
			}else{
				$(".dialogmsg").text(data.message);
				//$('#myModal').modal('hide');
			}
			
		}
		G(objectModel,complete);
	});	
	
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$('#myModal').modal('hide');
	});
	$(".canceldialog2").click(function(){//cancel
		$('#myModal2').modal('hide');
		$('#myModal').modal('show');
	});
}

clear_dialog = function(){
	$("#form2 input[type=reset]").click();
	$("#dialog_save").attr("disabled",false);
	$(".dialogmsg").text("");
	$("#email").attr("disabled",false);
	
	$(".emsg").html("<span class='label'>邮箱需要认证</span>");
	if(objectModel.edit_status=="create"){
		objectModel.rolecode = "editor";
		$(".setrole").each(function(kev,value){
			if($(this).attr("key")==objectModel.rolecode){
				$(this).click();
			}
		});
	}
		
}

open_dialog = function(data){
	clear_dialog();
	switch(objectModel.edit_status){
		case "create":
			$(".dialog_status").text("新增");
			break;
		case "update":
			$(".dialog_status").text("更新");

			$.each(data, function(index, value) {
				$("#"+index).val(value);
			});
			$("#email").attr("disabled",true);
			objectModel = data;
			objectModel.edit_status = "update";
			objectModel.objectid = objectModel.uid;

			$(".setrole").each(function(kev,value){
				if($(this).attr("key")==objectModel.rolecode){
					$(this).click();
				}
			});
			
			$(".setstatus").each(function(kev,value){
				if($(this).attr("key")==objectModel.status){
					$(this).click();
				}
			});
			break;
	}
	$('#myModal').modal('show');
}

get = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			objectModel.edit_status = "update";
			open_dialog (data.data);
		}else{
			alert("ERROR adminuser 404");
		}
	}
	var url = site.web+site.module+"/get";
	var json = {url:url,objectid:id,uid:site.uid};
	G(json,cb);
}

del = function(arg){
	var name =  $(arg).attr("data-name");
	if(confirm("确认移除"+name+"?")==false)return;
	var id = $(arg).attr("data-id");
	cb = function (data){
		mpage.init();
	}
	var url = site.web+site.module+"/del";
	var json = {url:url,objectid:id,uid:site.uid};
	G(json,cb);
}


